/******************************************************************************************
 * Import
 ******************************************************************************************/

// module
import Taro, { Component } from '@tarojs/taro';
import { Swiper } from '@tarojs/components';
import { AtActionSheet, AtActionSheetItem } from "taro-ui"

// project
import { CPage, CContent, CHeader, CFooter, CFloat, CAround, CBetween, CCenter, CRow, CWrap, CLine, CHand, CItemX, CItemY, CText, CLabel, CSvg, CImage, CButton, CColumn } from '../../library/comp';
import { Base64, Gen, Time, Zip, Session, Storage, Toast} from '../../library/func';
import Icon from '../../project/style/icon';
import Http from '../../project/func/http/http';
import '../../library/style/patch.css';

import { Addcbtn, Cbtn, Chead, Destop, Editcunit, Ibottom, Ibottomp, Listunitone, Listunittwo, LogoLine, LogoLinep, Tabheader } from '../../project/comp';


/******************************************************************************************
 * Export Class
 ******************************************************************************************/

export default class EditIndex extends Component {

	constructor(props) {
		super(props);
		this.state = {

			is_master:false,

			article:[],
			activity:[],
			system:{},
			dian:true,
			addshow:false,
			showtab:1,
		}

	}


	onShareAppMessage(res){
		return{
			title: res.target.dataset.title,
			path: res.target.dataset.path,
			imageUrl:res.target.dataset.title,
		}
	}



	//获取列表
	getList(callback){

		Http.AuthOk((res)=>{
			this.setState({
				is_master:res
			})
		})


		Http.Loading({
			url:'/home/home.php',
			success:(res)=>{
				this.setState({
					article:res.article,
					activity:res.activity,
					system:res.system['object'],
				})
				if(callback){
					callback();
				}
			}
		})
	}

	componentWillMount () {
		this.getList();
	}	

    componentDidMount() {
		this.getList();
    }

	componentDidShow(){
		this.getList();
	}

	
  // onRedirect
	onRedirect(url){
		wx.redirectTo({
			url: url
		})
	}	

	// onNavigate
	onNavigate(url){
		wx.navigateTo({
			url: url
		})
	}

	//onNavigateBack
	onNavigateBack(num=1){
		wx.navigateBack({
			delta: num
		})                    
	}


	//添加文章或者活动
	onAdd(){
		this.setState({
			addshow:true,
		})
	}

	//设置tab1
	onSetTabone(){
		this.setState({
			showtab:1,
		})
	}
	//设置tab2
	onSetTabtwo(){
		this.setState({
			showtab:2,
			dian:false,
		})
	}

	//onCancel 取消添加活动或者动态的碳层
	onCancel(){
		this.setState({
			addshow:false,
		})

	}

	//点赞
	onGoodClick(type, item){
		
		Storage.get({
			key: type +'good',
			success:(res)=>{
				if(res){
					console.log(res)
					if(res.indexOf(item.id) != -1){
						Toast.error('请勿重复点赞');
					}else{
						doit(type, item);
						let arr = res;
						arr.push(item.id);
						Storage.set({
							key: type +'good',
							value:arr,
						})	
					}
				}else{
					doit(type, item);
					let arr = [];
					arr.push(item.id);
					Storage.set({
						key: type +'good',
						value:arr,
					})
				}
			}
		})


		var doit = () =>{
			Http.post({
				url:'/good/logic/add.php',
				data:{
					count_good:item.count_like,
					id:item.id,
					type:type,
				},
				success:(res)=>{
					this.getList(()=>{
						Toast.success('点赞成功');
					});
				}
			})	
		}

	}


	//置顶
	onTob(type, item){
		Http.post({
			url:'/istop/logic/istop.php',
			data:{
				id:item.id,
				type:type,
			},
			success:(res)=>{
				this.getList(()=>{
					Toast.success('置顶成功');
				});
			}
		})	
	}

	Add(url){
		this.onNavigate(url);
		this.onCancel();
	}


  render() {

	let show = false;
	
	let list = this.state.showtab == 1 ? this.state.article : this.state.activity;

	let type = this.state.showtab == 1 ? '动态' : '活动';

	let url = this.state.showtab == 1 ? '/screen/article/detail?type=article&id=' : '/screen/activity/detail?type=activity&id=';

    return (
		<CPage>
			<CContent style='height:100vh'>

				<LogoLine 
				setname={ this.state.system.set_name }
				setlogo={ this.state.system.set_logo }
				setdescription={ this.state.system.set_description }
				></LogoLine>
				<Tabheader 
				onSearch={ this.onNavigate.bind(this, '/screen/home/search') } 
				onAdd={ this.onAdd }
				onTabone={ this.onSetTabone }
				onTabtwo={ this.onSetTabtwo }
				dian={ this.state.dian }
				is_master = { this.state.is_master }
				></Tabheader>
				{
					list.map((item, key)=>{
						return(
							this.state.is_master ? 
							
							this.state.showtab == 1 ?
							<Listunitone 
							is_master = { this.state.is_master }
							data={ item } 
							onGoodClick={ this.onGoodClick.bind(this, type, item) }
							shareurl={ url + item.id }
							onTob={ this.onTob.bind(this, type, item) }
							></Listunitone>
							:
							<Listunittwo 
							is_master = { this.state.is_master }
							data={ item } 
							url={ url + item.id }
							onTob={ this.onTob.bind(this, type, item) }
							></Listunittwo>
							:
							item.set_authority?
							this.state.showtab == 1 ?
							<Listunitone 
							is_master = { this.state.is_master }
							data={ item } 
							onGoodClick={ this.onGoodClick.bind(this, type, item) }
							shareurl={ url + item.id }
							onTob={ this.onTob.bind(this, type, item) }
							></Listunitone>
							:
							<Listunittwo 
							is_master = { this.state.is_master }
							data={ item } 
							url={ url + item.id }
							onTob={ this.onTob.bind(this, type, item) }
							></Listunittwo>
							:
							<view></view>
						)
					})
				}


			</CContent>

			<AtActionSheet isOpened={ this.state.addshow }  onClose={this.onCancel} cancelText='取消' title="添加">
				<AtActionSheetItem onClick={ this.Add.bind(this, '/screen/add/list?type=article') }>
					添加动态
				</AtActionSheetItem>
				<AtActionSheetItem onClick={ this.Add.bind(this, '/screen/activity/set') }>
					添加活动
				</AtActionSheetItem>
			</AtActionSheet>

		</CPage>
    );
  }
}
